package cn.zy.amyan.client.util;

import cn.zy.amyan.client.AyClient;
import cn.zy.amyan.client.factory.BeanFactory;
import cn.zy.amyan.core.data.ProductInfo;
import cn.zy.amyan.server.interfaces.IAyServer;

/**
 * @author 子言
 * @date 2024/5/5 下午 18:34
 */
public class AyClientUtil {

	// 作为注册中心客户端启动，想注册中心注册信息
	private static final AyClient centerClient = new AyClient();
	private static IAyServer server = null;
	
	public static boolean register(ProductInfo productInfo) {
		init();
		return server.register(productInfo);
	}
	
	public static ProductInfo queryServer(String serverName) {
		init();
		return server.queryServer(serverName);
	}
	
	public static <T> T getBean(String serverName, Class<T> clazz){
		ProductInfo productInfo = queryServer(serverName);

		// 作为注册中心客户端启动，想注册中心注册信息
		AyClient consumer = new AyClient();
		// 注册中心ip，端口
		consumer.start(productInfo.getHost(), productInfo.getPort());
		BeanFactory factory = new BeanFactory(consumer);
		return factory.getBean(clazz);
	}
	
	private static void init() {
		// 注册中心ip，端口，可通过配置获取
		centerClient.start("127.0.0.1", 5566);
		BeanFactory beanFactory = new BeanFactory(centerClient);
		server = beanFactory.getBean(IAyServer.class);
	}
	
}
